<template>
  <div class="mod-menu">
    <el-form :inline="true" :model="params">
      <el-form-item label="商品名称">
        <el-input v-model="params.title" placeholder="商品名称" clearable></el-input>
      </el-form-item>
      <el-form-item label="用户昵称">
        <el-input v-model="params.nickname" placeholder="用户昵称" clearable></el-input>
      </el-form-item>
      <el-form-item label="评价内容">
        <el-input v-model="params.content" placeholder="评价内容" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="search()">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="dataList" v-loading="tableLoading" row-key="Id" border style="width: 100%; ">
      <el-table-column prop="nickname" align="center" label="用户昵称">
      </el-table-column>
      <el-table-column prop="title" align="center" label="商品名称">
      </el-table-column>
      <el-table-column prop="level" align="center" label="评价">
        <template slot-scope="scope">
          <el-rate v-model="scope.row.level" disabled text-color="#ff0000">
          </el-rate>
        </template>
      </el-table-column>
      <el-table-column prop="content" align="center" label="评价内容">
      </el-table-column>
      <el-table-column prop="create_time" align="center" label="评论时间">
      </el-table-column>
      <el-table-column prop="show" align="center" label="是否显示">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.show" :active-value="true" inactive-color="#B9B9B9" @change="switchChange(scope.row)" />
        </template>
      </el-table-column>

      <el-table-column fixed="right" header-align="center" align="center" label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row)">查看</el-button>
          <el-button type="text" size="small" @click="reply(scope.row)" v-if="!scope.row.is_reply">回复</el-button>

          <el-button type="text" size="small" class="c-error" @click="businessDelgoods(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="params.page" :page-sizes="[10, 20, 50, 100]" :page-size="params.limit" :total="totalPage" layout="total, sizes, prev, pager, next, jumper"></el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <evaluate-look v-if="addOrUpdateVisible" ref="evaluateLook"></evaluate-look>

    <el-dialog title="回复" :visible.sync="dialogVisible" width="60%" :before-close="handleClose">
      <el-input type="textarea" :rows="2" placeholder="请输入内容" maxlength="200" v-model="textarea">
      </el-input>
      <span slot="footer" class="dialog-footer">

        <el-button type="primary" @click="replySave()">确 定</el-button>
      </span>

    </el-dialog>
  </div>
</template>

<script>
import evaluateLook from './evaluate-look.vue'
import serviceApione from '@/api/serviceApione.js'
import serviceApi from '@/api/serviceApi.js'
export default {
  data () {
    return {
      dataForm: {},
      dataList: [],
      dataListLoading: false,
      tableLoading: false,
      addOrUpdateVisible: false,
      totalPage: 0,
      params: {
        page: 1,
        limit: 10,
        title: '',
        content: '',
        nickname: ''
      },
      select: null,
      textarea: null,
      dialogVisible: false
    }
  },
  components: {
    evaluateLook
  },
  mounted () {
    this.getData()
  },
  methods: {
    handleClose () {
      this.dialogVisible = false
    },
    replySave () {
      serviceApi.replySave({ content: this.textarea, comment_id: this.select.id }).then(res => {
        if (res && res.data.code == 200) {
          this.$message.success('操作成功')
          this.dialogVisible = false
          this.getData()
        }
      })
    },
    reply (row) {
      this.select = row
      this.dialogVisible = true
    },
    switchChange (row) {
      let msg = ''
      let param = {
        id: row.id,
        show: row.show
      }
      if (row.show) {
        msg = '已显示'
      } else {
        msg = '已隐藏'
      }
      serviceApione.commotPutAway(param).then(res => {
        if (res.data && res.data.code == 200) {
          this.$message.success(msg)
        } else {
          row.show = !row.show
        }
      })
    },
    // 每页数
    sizeChangeHandle (val) {
      this.params.page = 1
      this.params.limit = val
      this.getData()
    },
    // 当前页
    currentChangeHandle (val) {
      this.params.page = val
      this.getData()
    },
    search () {
      this.params.page = 1
      this.getData()
    },
    getData () {
      this.tableLoading = true
      serviceApione.getCommentList(this.params).then(res => {
        if (res && res.data.code == 200) {
          this.dataList = res.data.page.list
          this.totalPage = res.data.page.totalCount
        }
        this.tableLoading = false
      })
    },
    businessDelgoods (row) {
      this.$confirm(`是否删除商品 [ ${row.title} ] 的评论?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        serviceApione.delcommontCard(row.id).then(res => {
          if (res.data && res.data.code == 200) {
            this.$message.success('删除成功！')
            this.getData()
          }
        })
      }).catch(() => { })
    },
    // 查看
    addOrUpdateHandle (row) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.evaluateLook.init(row)
      })
    }
  }
}
</script>
